package com.etc.team2.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.etc.team2.service.StationService;

public class StationExistServlet extends HttpServlet {

	/**
	 * 检查用户键入的站台名是否存在
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String referer=request.getHeader("referer");
		if (referer==null||!referer.startsWith("http://localhost:")) {
			response.sendRedirect("home.jsp");
			return;
		}
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		String stationName=request.getParameter("station");
		StationService ss=new StationService();
		if(ss.getStation(stationName)!=null)
		{
			JSONObject object=new JSONObject();
			try {
				object.put("words", "yes");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setContentType("text/html;charset=utf-8");
			System.out.println(object);
			out.print(object);
		    out.close();
		}else
		{
			JSONObject object=new JSONObject();
			try {
				object.put("words", "no");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setContentType("text/html;charset=utf-8");
			System.out.println(object);
			out.print(object);
		    out.close();
		}
		//4.数据的返回
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
